package com.rock.cms.controller;

import com.eova.common.Easy;
import com.eova.common.base.BaseController;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.rock.cms.model.TCmArticle;

import cn.hutool.core.util.StrUtil;

public class RockCmsController extends BaseController {

	/**
	 * 初始化微官网数据
	 */
	public void initCmsData() {
		String appId = getPara(0);
		if(StrUtil.isBlank(appId)) {
			renderJson(new Easy("参数非法，未传递appId"));
			return;
		}
		
		Record res = Db.findFirst("select t.template_app_id,a.tenant_id from t_s_app a,t_s_app_type t where a.id=? and a.type_id=t.id",appId);
		
		if(res == null) {
			renderJson(new Easy("导入数据失败，未找到模板appId"));
			return;
		}
		
		String srcAppId = res.getStr("template_app_id");
		String tenantId = res.getStr("tenant_id");
		
		//模板app禁止自行导入操作
		if(srcAppId.equalsIgnoreCase(appId)) {
			renderJson(new Easy("导入数据失败，模板禁止导入"));
			return;
		}
		
		//导入文章分类及文章
		CmsUtil.copyCategoryAndArticle(srcAppId,appId,tenantId);
		
		//导入轮播图
		//查找该APP下一篇文章作为轮播图跳转文章
		TCmArticle art = TCmArticle.dao.findFirst("select * from t_cm_article where tenant_id=? and app_id=?",tenantId,appId);
		int articleId = 0;
		if(art != null) {
			articleId = art.getId().intValue();
		}
		CmsUtil.copyBannerData(srcAppId,appId,tenantId,articleId);

		renderJson(new Easy("微官网数据导入成功",true));

	}
	
}
